c++ accumulate with move instead of copy [migrated]

Posted by user74399 on Programmers See other posts from Programmers or by user74399
Published on 2012-12-05T13:56:29Z Indexed on 2012/12/05 17:26 UTC
Read the original article Hit count: 87

Filed under:
|

I have the following code

auto adder = [](string& s1, const string& s2)->string&&
   {
      if (!s1.empty())
         s1 += " ";
      s1 += s2;
      return move(s1);
   };

   string test;
   test.reserve(wordArray.size() * 10);
   string words = accumulate(wordArray.begin(), wordArray.end(), move(test), adder);

What I would like here is to avoid string copying. Unfortunately this is not accomplished by the vs2012 implementation of accumulate. Internally accumulate calls another function _Accumulate and the rvalue functionality gets lost in the process.

It I instead call the _Accumulate function like so

string words = Accumulate(wordArray.begin(), wordArray.end(), move(test), adder);

I get the intended performance gain.

Must the std library be rewritten to take rvalue arguments into consideration?

Is there some other way I may use accumulate to accomplish what I want without cheating to much?

© Programmers or respective owner

Related posts about c++

Related posts about visual-c++